/**
 * 
 */
package com.ssm.util.security;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;

import com.ssm.exception.WebException;
import com.ssm.model.Model;
import com.ssm.model.bean.SsmGroups;
import com.ssm.model.bean.SsmUserRules;
import com.ssm.model.mapper.GroupsMapper;
import com.ssm.model.mapper.UserRuleMapper;
import com.ssm.util.security.Interface.AbstractFilterChainDefinitionsService;

import tk.mybatis.mapper.entity.Example;
import tk.mybatis.mapper.entity.Example.Criteria;

/**
 * 
 * @author 危文涛<245108903@qq.com>
 * @version 2016年2月28日 下午6:39:07
 *
 */
public class SimpleFilterChainDefinitionsService extends AbstractFilterChainDefinitionsService {

	@Autowired(required=true)
	private Model M;

	@Override
	public Map<String, String> initOtherPermission() {
		// TODO Auto-generated method stub
		Map<String,String> permisions= new HashMap<String, String>(); 
		/**/UserRuleMapper userRuleMapper=M.getMapper(UserRuleMapper.class);
		GroupsMapper groupsMapper=M.getMapper(GroupsMapper.class);
		Example example=new Example(SsmGroups.class);
		Criteria criteria=example.createCriteria();
		criteria.andCondition("status=1");
		List<SsmGroups> groupList=groupsMapper.selectByExample(example);
		for (SsmGroups ssmGroups : groupList) {
			String groupName=ssmGroups.getName();
			String rules=ssmGroups.getRule();
			List<Integer> ruleList=new ArrayList<Integer>();
			for(String rule:rules.split("%"))
			{
				try {
					ruleList.add(Integer.parseInt(rule));
				} catch (Exception e) {
					throw new WebException("规则id转换int错误");
				}
			}
			example=new Example(SsmUserRules.class);
			criteria=example.createCriteria();
			criteria.andIn("id",ruleList);
			List<SsmUserRules> userRuleList= userRuleMapper.selectByExample(example);
			for(SsmUserRules userRule:userRuleList)
			{
				String t="orRole["+groupName+"]";
				if(permisions.containsKey(userRule.getLink()))
				{
					t=permisions.get(userRule.getLink());
					t=t.substring(0,t.length()-1);
					t+=","+groupName+"]";
				}
				permisions.put(userRule.getLink(),t);
			}
		}
		return permisions;
	}
	
	@Override
	public void setDefinitions(String definitions) {
		// TODO Auto-generated method stub
		super.setDefinitions(definitions);
	}

}
